﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.SqlServerCe;

namespace WPF_DbCenter.SqlCE.AdditionUI
{
    /// <summary>
    /// Interaction logic for ChangeSdfPwd.xaml
    /// </summary>
    public partial class ChangeSdfPwd : Window
    {

        public string NewPwd { get; set; }
        public string Db { get; set; }
        public ChangeSdfPwd(string db)
        {
            Db = db;
            InitializeComponent();
        }

        private void butChange_Click(object sender, RoutedEventArgs e)
        {
            string oldPwd = txtPwd.Password;
            string newPwd = passwordBox2.Password;
            string newPwd2 = passwordBox3.Password;
            if (!newPwd.Equals(newPwd2))
            {
                MessageBox.Show("Your new password are not equal ,please input again ");
                return;
            }

            try
            {
                using (SqlCeEngine eg = new SqlCeEngine())
                {
                    eg.LocalConnectionString = String.Format("Data Source={0};Password={1}", Db, oldPwd);
                    eg.Compact(string.Format("Data Source={0};Password={1}", Db, newPwd));
                }
                MessageBox.Show("Change password successful");

                NewPwd = newPwd;
            }
            catch (SqlCeException ee)
            {
                MessageBox.Show(String.Format("{0}\r\n{1}", ee.Message, "This may the old password you input is not valid"));
                return;
            }

            DialogResult = true;
            
        }
    }
}
